import { defineStore } from 'pinia'
import store from '../pinia'
import { nextTick } from 'vue'
import DictService from '@/service/system/dict'
import { isEmptyObject } from '@/utils'

const useDictStore = defineStore('dict', {
  state: () => {
    return {
      dict: {} as any,
    }
  },
  actions: {
    getDicts() {
      return new Promise<void>((resolve) => {
        DictService.getDicts({}).then(res => {
          if (res.code == 200) {
            this.dict = res.data
          }
          resolve()
        })
      })
    },
    isEmptyDict() {
      return !this.dict || isEmptyObject(this.dict)
    },
  },
  presist: {
    enable: true,
    resetToState: true,
    option: {

    },
  },
})

export default useDictStore

/**
 * 返回一个Promise对象，目的是防止在非vue组件使用中的时候出现插件还没有出初始化完成导致持久化操作失败的bug。
 * 使用方式：
 *  import { useDictStoreContext } from '@/store/modules/dict'
    useDictStoreContext().then((store) => {
      console.log(store.dict)
    })
 * @returns Promise<typeof useDictStore>
 */
export async function useDictStoreContext() {
  await nextTick()
  return useDictStore(store)
}
    